
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>

int main()
{
    int ret;
    struct input_event key_evt;

    int fd = open("/dev/input/event0", O_RDWR);
    if(fd == -1) {
        perror("open");
        exit(-1);
    }

    printf("open successed!fd = %d\n", fd);

    while(1) {
        // 读取键值
        ret = read(fd, &key_evt, sizeof(key_evt));
        if(ret < 0) {
            perror("read");
            break;
        }

        if(key_evt.type == EV_KEY) {
            switch(key_evt.code) {
                case KEY_OK: printf("KEY_OK %s \n", key_evt.value ? "pressed" : "released"); break;
                case KEY_SELECT: printf("KEY_SELECT %s \n", key_evt.value ? "pressed" : "released"); break;
                case KEY_VOLUMEUP: printf("KEY_VOLUMEUP %s \n", key_evt.value ? "pressed" : "released"); break;
                case KEY_VOLUMEDOWN: printf("KEY_VOLUMEDOWN %s \n", key_evt.value ? "pressed" : "released"); break;
                default: printf("unknow input!\n"); break;
            }
        }
    }

    close(fd);
    return 0;
}